package handle

import (
	"encoding/json"
	"log"
	util "my_doc_service/util"
	"net/http"
	"strings"
)

type MyHandler struct {
	StaticPath string
}

func (self MyHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	log.Println("请求路径:", request.URL.Path)
	if strings.HasPrefix(request.URL.String(), "/public/") {
		had := http.StripPrefix("/public/", http.FileServer(http.Dir(self.StaticPath)))
		had.ServeHTTP(writer, request)
	} else {
		switch request.URL.Path {
		case "/api/getDocList":
			writer.Header().Set("content-type", "text/json")
			result, _ := util.GetDocList(self.StaticPath + "/doc")
			jsonBytes, _ := json.Marshal(result)
			writer.Write(jsonBytes)
		default:
			http.NotFound(writer, request)
		}
	}
}
